package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.RedisConstant;
import com.itheima.sdk.WXPay;
import com.itheima.service.PayService;
import com.itheima.utils.MyWXPayConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 吴
 * @version V1.0
 * @Description
 * @Package com.itheima.service.impl
 * @date 2021/12/9 15:46
 */
@Service(interfaceClass = PayService.class)
public class PayServiceImpl implements PayService {


    @Override
    public Map<String, Object> generateUrl(String price,String setmealId) throws Exception {
        MyWXPayConfigImpl config = new MyWXPayConfigImpl();
        WXPay wxpay = new WXPay(config);
        // 订单号
        String  orderNumber = UUID.randomUUID().toString().replace("-", "");

        // 结束时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.MINUTE,1);
        String format = new SimpleDateFormat("yyyyMMddHHmmss").format(calendar.getTime());


        Map<String, String> data = new HashMap<String, String>();
        data.put("body", "商品支付测试");
        data.put("out_trade_no", orderNumber);
        data.put("fee_type", "CNY");
        data.put("total_fee", "1"); // 单位：分
        data.put("spbill_create_ip", "123.12.12.123");
        data.put("notify_url", "http://fjayme.natappfree.cc/wxpay/notify");
        data.put("trade_type", "NATIVE");  // 此处指定为扫码支付
        data.put("product_id", setmealId);
        data.put("time_expire",format);
        Map<String, String> resp = wxpay.unifiedOrder(data);
        System.out.println(resp);

        Map<String, Object> map = new HashMap<>();
        map.put("codeUrl",resp.get("code_url"));
        map.put("orderNumber",orderNumber);

        return map;
    }

    /**
     * 根据订单号查询订单信息
     *
     */
    @Override
    public String findByOrderNumber(String orderNumber) throws Exception {
        MyWXPayConfigImpl config = new MyWXPayConfigImpl();
        WXPay wxpay = new WXPay(config);

        Map<String, String> data = new HashMap<String, String>();
        data.put("out_trade_no", orderNumber);
        Map<String, String> resp = wxpay.orderQuery(data);
        System.out.println(resp);
        return resp.get("trade_state_desc");
    }
}
